// 引入 腾讯地图 Api SDK核心类
var QQMapWX = require('../js/qqmap-wx-jssdk.min.js')
// 实例化API核心类
var demo = new QQMapWX({
	key: 'EP2BZ-NBFKS-MOSO6-6GUD3-7BGRV-LIFIO' // 必填
})
Page({
  data: {
		msg:'',
		map: false,
		latitude: '',
		longitude: '',
		markers: []
  },
	onReady: function(){
		this.mapCtx = wx.createMapContext('route')
		this.includePoints()
	},
	includePoints: function () {
		var arr = []
		var _this = this
		wx.getStorage({
			key: 'gojson',
			success: function (res) {
				var obj = {}
				obj.iconPath = "../images/ic_position_go.png";
				obj.id = 0;
				obj.latitude = res.data.latitude;
				obj.longitude = res.data.longitude;
				obj.width = 40;
				obj.height = 40;
				arr.push(obj)
				wx.getStorage({
					key: 'tojson',
					success: function (res) {
						var obj = {}
						obj.iconPath = "../images/ic_position_to.png";
						obj.id = 0;
						obj.latitude = res.data.latitude;
						obj.longitude = res.data.longitude;
						obj.width = 40;
						obj.height = 40;
						arr.push(obj)
						_this.setData({
							markers: arr,
							map: true
						})
						_this.mapCtx.includePoints({
							padding: [10],
							points: [{
								latitude: _this.data.markers[0].latitude,
								longitude: _this.data.markers[0].longitude,
							}, {
								latitude: _this.data.markers[1].latitude,
								longitude: _this.data.markers[1].longitude,
							}]
						})
						// 腾讯地图api

						demo.calculateDistance({
							mode: 'driving', // 可选值：'driving'（驾车）、'walking'（步行），默认：'walking'
							from: { // 默认是当前位置
								latitude: _this.data.markers[0].latitude,
								longitude: _this.data.markers[0].longitude
							},
							to: [{
								latitude: _this.data.markers[1].latitude,
								longitude: _this.data.markers[1].longitude,
							}],
							success: function (res) {
								_this.setData({
									msg: res.status
								})
							},
							complete: function (res) {
								_this.setData({
									msg: res.status
								})
							}
						});
					}
				})
			}
		})
		
		
	}
})